﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_Them_CapNhat_NXB : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void ThemMoi();        
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoi = null;
        public event CapNhat KhiCapNhat = null;

        public string Action = "";
        public string ID = null;

        NhaXuatBan_BUS nxb_bus;
        NhaXuatBan_DTO nxb_dto;

        public Form_Them_CapNhat_NXB()
        {
            InitializeComponent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {            
            nxb_bus = new NhaXuatBan_BUS();
            nxb_dto = new NhaXuatBan_DTO();
            nxb_dto.Str_MaNXB = txtMaNXB.Text;
            nxb_dto.Str_TenNXB = txtTenNXB.Text;
            nxb_dto.Str_DiaChi = txtDiaChi.Text;
            nxb_dto.Str_DienThoai = txtDienThoai.Text;
            nxb_dto.Str_Email = txtEmail.Text;
            nxb_dto.Str_TrangThai = 0;//luc them moi thi nxb o trang thai chua xoa
            //them nha xuat ban
            if (Action.Equals("Insert"))
            {
                if (nxb_bus.ThemNXB(nxb_dto) == 1)
                {
                    MessageBox.Show("Thêm thành công", "Thông báo");
                    if (KhiThemMoi != null)
                    {
                        KhiThemMoi();
                        Form_Them_CapNhat_NXB_Load(sender, e);
                        txtMaNXB.Text = nxb_bus.MaTang();
                    }
                }
                else
                    MessageBoxEx.Show("Thêm thất bại","Thông báo");
            }
            //cap nhat nha xuat ban
            if (Action.Equals("Update"))
            {
                if (nxb_bus.CapNhatNXB(nxb_dto) == 1)
                {
                    MessageBoxEx.Show("Cập nhật thành công", "Thông báo");
                    if (KhiCapNhat != null)
                    {
                        KhiCapNhat();
                        this.Close(); 
                    }
                }
                else
                    MessageBoxEx.Show("Cập nhật thất bại", "Thông báo");
            }      
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_Them_CapNhat_NXB_Load(object sender, EventArgs e)
        {
            //set man hinh luc loadform
            SetBounds(Screen.GetWorkingArea(this).Width/4, Screen.GetWorkingArea(this).Height/4, Width, Height);
            //them nha xuat ban
            if (Action.Equals("Insert"))
            {
                nxb_bus = new NhaXuatBan_BUS();
                txtMaNXB.Text = nxb_bus.MaTang().ToString();
            }
            //cap nhat nha xuat ban
            if (Action.Equals("Update"))
            {
                nxb_bus = new NhaXuatBan_BUS();
                nxb_dto = nxb_bus.DanhSachNhaXuatBan_TheoMaNXB(ID);
                txtMaNXB.Text = nxb_dto.Str_MaNXB;
                txtTenNXB.Text = nxb_dto.Str_TenNXB;
                txtDiaChi.Text = nxb_dto.Str_DiaChi;
                txtDienThoai.Text = nxb_dto.Str_DienThoai;
                txtEmail.Text = nxb_dto.Str_Email;
            }
        }
    }
}